import os
import shutil

base_path = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

def get_output_dir(job_id):
    return os.path.join(base_path, 'outputs', job_id)

def get_temp_dir(path):
    return os.path.join(path, 'temp')

def get_screenshot_dir(path):
    return os.path.join(path, 'screenshot')

def get_log_dir(path):
    return os.path.join(path, 'log')

def get_adb_path():
    return os.path.join(base_path, 'platform-tools', 'adb.exe')

def init_output_dir(job_id):
    output_dir = get_output_dir(job_id)
    if os.path.exists(output_dir):
        shutil.rmtree(os.path.join(output_dir, 'temp'))
        shutil.rmtree(os.path.join(output_dir, 'screenshot'))
        shutil.rmtree(os.path.join(output_dir, 'log'))
        # shutil.rmtree(output_dir)
    mkdir(output_dir)
    mkdir(os.path.join(output_dir, 'temp'))
    mkdir(os.path.join(output_dir, 'screenshot'))
    mkdir(os.path.join(output_dir, 'log'))

    return output_dir

def rename(path, new_path):
    if os.path.exists(new_path):
        os.remove(new_path)
    if os.path.exists(path):
        os.rename(path, new_path)


def mkdir(path):
    if not os.path.exists(path):
        os.makedirs(path)

    return path